package cli

import (
	"github.com/ihaiker/tfwrapper/internal/orch"
	"github.com/ihaiker/tfwrapper/internal/parser"
	"github.com/ihaiker/tfwrapper/internal/util"
	"github.com/urfave/cli/v2"
)

func showHelp(fn func(*cli.Context) error) func(ctx *cli.Context) error {
	return func(ctx *cli.Context) error {
		if ctx.Args().Len() == 0 {
			return cli.ShowAppHelp(ctx)
		}
		return fn(ctx)
	}
}

var run = &cli.Command{
	Name: "run", Usage: "run terraform command",
	UsageText: "run terraform command",
	Hidden:    true, SkipFlagParsing: true,
	Action: showHelp(func(ctx *cli.Context) error {
		wrapper, err := parser.Parser(ctx.App.Version, *flags)
		if util.HasErrors(err) {
			return err
		}
		return orch.Run(wrapper, ctx.Args().Slice())
	}),
}

func init() {
	app.Commands = append(app.Commands, run)
}
